package jehc.djshi.sys.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jehc.djshi.common.base.BaseService;
import jehc.djshi.common.util.ExceptionUtil;
import jehc.djshi.log.client.service.LogsUtil;
import jehc.djshi.sys.dao.XtPostDao;
import jehc.djshi.sys.model.XtPost;
import jehc.djshi.sys.service.XtPostService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @Desc 用户岗位
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class XtPostServiceImpl extends BaseService implements XtPostService {

	@Resource
	XtPostDao xtPostDao;

	@Resource
	LogsUtil logsUtil;

	/**
	 * 分页
	 * @param condition
	 * @return
	 */
	public List<XtPost> getXtPostListByCondition(Map<String,Object> condition){
		try {
			return xtPostDao.getXtPostListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 查询对象
	 * @param id
	 * @return
	 */
	public XtPost getXtPostById(String id){
		try {
			return xtPostDao.getXtPostById(id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 添加
	 * @param xtPost
	 * @return
	 */
	public int addXtPost(XtPost xtPost){
		int i = 0;
		try {
			xtPost.setCreateId(getXtUid());
			xtPost.setCreateTime(getDate());
			i = xtPostDao.addXtPost(xtPost);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("岗位业务类", "添加", "添加岗位成功");
		} catch (Exception e) {
			logsUtil.aBLogs("岗位业务类", "添加", "添加岗位失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 修改
	 * @param xtPost
	 * @return
	 */
	public int updateXtPost(XtPost xtPost){
		int i = 0;
		try {
			xtPost.setUpdateId(getXtUid());
			xtPost.setUpdateTime(getDate());
			i = xtPostDao.updateXtPost(xtPost);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("岗位业务类", "修改", "修改岗位成功");
		} catch (Exception e) {
			logsUtil.aBLogs("岗位业务类", "修改", "修改岗位失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 删除
	 * @param condition
	 * @return
	 */
	public int delXtPost(Map<String,Object> condition){
		int i = 0;
		try {
			String[] idList = (String[])condition.get("id");
			//验证下级岗位是否存在
			for(String id:idList){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("parentId", id);
				if(!xtPostDao.getXtPostListChild(map).isEmpty()){
					logsUtil.aBLogs("岗位业务类", "删除", "执行删除岗位，编号【"+id+"】存在下级岗位，不能删除");
					return 0;
				}
			}
			i = xtPostDao.delXtPost(condition);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("岗位业务类", "删除", "删除岗位成功");
		} catch (Exception e) {
			logsUtil.aBLogs("岗位业务类", "删除", "删除岗位失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 岗位根目录集合
	 * @return
	 */
	public List<XtPost> getXtPostInfoList(Map<String,Object> condition){
		return xtPostDao.getXtPostInfoList(condition);
	}

	/**
	 * 查找子集合
	 * @return
	 */
	public List<XtPost> getXtPostListChild(Map<String,Object> condition){
		try {
			return xtPostDao.getXtPostListChild(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 查找所有集合
	 * @return
	 */
	public List<XtPost> getXtPostListAll(Map<String,Object> condition){
		try {
			return xtPostDao.getXtPostListAll(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 根据各种情况查找集合不分页（流程设计器中处理组 发起组等使用）
	 * @param condition
	 * @return
	 */
	public List<XtPost> getXtPostList(Map<String,Object> condition){
		return xtPostDao.getXtPostList(condition);
	}

	/**
	 * 非根岗位全部集合
	 * @param condition
	 * @return
	 */
	public List<XtPost> getXtPostInfoUnRootList(Map<String,Object> condition){
		return xtPostDao.getXtPostInfoUnRootList(condition);
	}
}
